package com.fm.financemanage.component;


import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;


/**
 * @version V1.0
 * @author: zpc
 * @date:
 */
@Service
public class QuartzComponent {
    @Autowired
    private Scheduler scheduler;


    @PostConstruct
    public void startScheduler() {
        try {
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public void getTrigger(Class<? extends QuartzJobBean> join, String cron, Map jobData, String jobName, String jobGroupName) {
        JobDetail jobDetail = JobBuilder.newJob(join).withIdentity(jobName, jobGroupName).build();
        if (jobData != null && jobData.size() > 0) {
            jobDetail.getJobDataMap().putAll(jobData);
        }
//        Set<Trigger> objects = new HashSet<>();
        Trigger trigger = TriggerBuilder.newTrigger().
                withIdentity(jobName, jobGroupName).
                withSchedule(CronScheduleBuilder.cronSchedule(cron))
                .forJob(jobDetail).build();
        try {
            scheduler.scheduleJob(jobDetail,trigger);
//            scheduler.scheduleJobs(triggersAndJobs,true);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public boolean addJob(Class<? extends QuartzJobBean> join, String jobName, String jobGroupName, Map jobData) {
        JobDetail build = JobBuilder.newJob(join).withIdentity(jobName, jobGroupName).build();
        //给新建的任务指定
        // 设置job参数
        if (jobData != null && jobData.size() > 0) {
            build.getJobDataMap().putAll(jobData);
        }
        /** 指定触发器规则
         * @Author zpc
         * @Date 12:29 2020/12/2
         * @Param [join, jobName 工作名称, jobGroupName 工作租名称, jobTime 定时的时间 , jobData 传入的参数]
         * startAt 代表当前任务在那个时间执行
         * @return boolean
         * withSchedule 指定两种时间调度器 SimpleScheduleBuilder(指定时间格式 date) 和 CronScheduleBuilder (指定时间格式 cron表达式)
         * withRepeatCount 给触发器指定重复次数   forJob 将指定的JobDetail加入Trigger  startAt代表在什么时候开启
         **/
        Trigger build1 = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroupName).withSchedule(SimpleScheduleBuilder.simpleSchedule()
                .withRepeatCount(0)).forJob(build).build();
        try {
            //开启调度器
            scheduler.scheduleJob(build, build1);
            //启动所有定时任务
//            scheduler.start();
            return true;
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return false;
    }

    //批量插入job
    public boolean addJobs(Map<JobDetail, Set<? extends Trigger>> map) {
        try {
            scheduler.scheduleJobs(map, true);
            //开启调度器

            return true;
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean updateJob(Class<? extends QuartzJobBean> join, String jobName, String jobGroupName, String cron, Map jobData) {
        TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroupName);
        try {
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            if (trigger != null && jobData != null && jobData.size() > 0) {
                //重新赋值消息内容
                trigger.getJobDataMap().putAll(jobData);
                trigger = trigger;
            } else {
                addJob(join, jobName, jobGroupName, jobData);
                return true;
            }
            CronTrigger build = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
            //重启调度器
            scheduler.rescheduleJob(triggerKey, build);
        } catch (SchedulerException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean removeJob(String jobName, String jobGroupName) {

        try {

            scheduler.deleteJob(new JobKey(jobName, jobGroupName));
        } catch (SchedulerException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
